﻿@using System.Data.Entity
@using Microsoft.Ajax.Utilities
@using QAS.Nekorama.Common
@using Resources

@model QAS.Nekorama.Models.EditQuestionModel
@{
    ViewBag.Title = QAS_Nekorama.EDIT; 
}
<script src="~/Scripts/jquery-te-1.4.0.min.js"> </script>
<link href="~/Content/jquery-te-1.4.0.css" rel="stylesheet" />
<script src="~/Scripts/jquery-ui-1.8.20.js"> </script>

<script src="~/Scripts/jquery.validationEngine.js"> </script>
<script src="@QAS_Nekorama.VALID_ENGINE"> </script>

<script>
    $(function() {
        var availableTags = [];

        function split(val) {
            return val.split(/,\s*/);
        }

        function extractLast(term) {
            return split(term).pop();
        }

        $("#alltag")
        // don't navigate away from the field on tab when selecting an item
            .bind("keyup", function(event) {
            $.ajax({
                url: '@Url.Action("GetTags", "Question")',
                dataType: 'json',
                data: { tags: $("#alltag").val() },
                    success: function(result) {
                    availableTags = result["result"];
                    var tags = $("#alltag").val().split(',');
                        $.each(tags, function(key, value) {
                        var index = $.inArray(value.trim(), availableTags);
                        if (index >= 0) {
                            availableTags.splice(index, 1);
                        }
                    });
                        if ($("#alltag").val().split(", ").length > 6) {
                            $("#message").validationEngine('showPrompt', '@Messages.TAG_LIMIT', 'error', 'topRight');
                        } else {
                            $("#message").validationEngine('hide');
                        }
                },
                    error: function(xhr) {
                    alert(xhr.statusText);
                }
            });
            if (event.keyCode === $.ui.keyCode.TAB &&
            $(this).data("ui-autocomplete").menu.active) {
                event.preventDefault();
            }
        })
         .autocomplete({
             minLength: 0,
                source: function(request, response) {
                 // delegate back to autocomplete, but extract the last term
                 response($.ui.autocomplete.filter(
                 availableTags, extractLast(request.term)));
             },
                focus: function() {
                 // prevent value inserted on focus
                 return false;
             },
                select: function(event, ui) {
                 var terms = split(this.value);
                 // remove the current input
                 terms.pop();
                 // add the selected item
                 terms.push(ui.item.value);
                 // add placeholder to get the comma-and-space at the end
                 terms.push("");
                 this.value = terms.join(", ");
                 if ($("#alltag").val().split(", ").length > 6) {
                     $("#message").validationEngine('showPrompt', '@Messages.TAG_LIMIT', 'error', 'topRight');
                    } else {
                        $("#message").validationEngine('hide');
                    }
                 return false;
             }
         });
    });

    $(document).ready(function() {
        $("#edit-q-form").validationEngine();
        checkdes();
        $(".jqte_editor").blur(function() {
            checkdes();
        });
        $("#bounty").blur(function() {
            //checkbt();
            if ($("#bounty").val() != null && parseInt($("#bounty").val()) >= @Helper.GetReputation(WebSecurity.CurrentUserName)) {
            $("#bounty").validationEngine('showPrompt', '@Messages.NOT_ENOUGH_PERMISSION', 'error', 'topRight', true);
            } else {
                $("#bounty").validationEngine('hide');
    }
    });
        });

        function showMess(event) {
            event.preventDefault();
            var flag = false;
            if ($("#txt").val().replace(/&nbsp;/g, '').replace(/<br>/g," ").trim().length < @Constants.QUESTION_CONTENT_MINIMUM)
            flag = true;
            if ($("#alltag").val().trim().split(", ").length > @Constants.NUMBER_TAG_MAXIMUM) 
            flag = true;
            if ($("#title").val().trim().length < @Constants.QUESTION_TITLE_MINIMUM)
                flag = true;
            if ($("#bounty").val().trim() != null && parseInt($("#bounty").val()) >= @Helper.GetReputation(WebSecurity.CurrentUserName))
            flag = true;
        if (flag) {
            checkTag();
            checkdes();
            checkbt();
            $("#edit-q-form").validationEngine('validate');
            event.preventDefault();
            return false;
        } else {
            $('#edit-q-form').submit();
            return false;
        }
    }

    function checkTag(){
        if ($("#alltag").val().split(", ").length > 5) {
            $("#message").validationEngine('showPrompt', '@Messages.TAG_LIMIT', 'error', 'topRight', true);
        }
    }
    function checkTag(){
        if ($("#alltag").val().split(", ").length > 5) {
            $("#message").validationEngine('showPrompt', '@Messages.TAG_LIMIT', 'error', 'topRight', true);
        }
    }
    function checkdes() {
        if ( $("#txt").val().replace(/&nbsp;/g, '').replace(/<br>/g," ").trim().length < @Constants.QUESTION_CONTENT_MINIMUM) {
            $(".jqte_editor").validationEngine('showPrompt', '@Messages.QUESTION_CONTENT_LENGTH', 'error', 'topRight', true);
        } else {
            $(".jqte_editor").validationEngine('hide');
        }
    }
    function checkbt() {
        if ($("#bounty").val() != null && parseInt($("#bounty").val()) >= @Helper.GetReputation(WebSecurity.CurrentUserName)) {
            $("#bounty").validationEngine('showPrompt', '@Messages.NOT_ENOUGH_PERMISSION', 'error', 'topRight', true);
    } else {
            $("#bounty").validationEngine('hide');
    }
    }
</script>

<div id="primary">
    <div id="primary-1">
        <!--ERROR LIST-->
                @if (TempData["error"] != null)
                {       
        <div class="error-list">
            <ul>
                <li>@TempData["error"]</li>
            </ul>
            </div>
            <!--end error-list-->
                    }
    </div>
    <!--end primary-1-->

    <form id="edit-q-form" method="post" action="Editquestion">
        <div id="primary-2">
            <input type="hidden" name ="qid" value="@Model.Question.QuestionID">
            
            <!--TITLE INPUT-->
            <h2 style="line-height: normal; padding-top: 5px;">@QAS_Nekorama.TITLE</h2>
            <input type="text" class="validate[required],minSize[10]" id="title" name="title" maxlength="225" value="@Model.Question.Title" />

            <!--CONTENT INPUT-->
            <input type="text"  id="txt"  name="content" value="@Model.Question.Content" />
            <script type="text/javascript"> $("#txt").jqte(); </script>

            <!--BOUNTY INPUT-->
            <h2 style="line-height: normal; padding-top: 5px;">@QAS_Nekorama.BOUNTY (@QAS_Nekorama.OPTIONAL)</h2>
            @if (Model.Question.BountyID.HasValue)
            {
                <input type="text" id="bounty" name="bounty" disabled="disabled" value="@Model.Bounty.Amount" />
            }
            else if (Helper.GetReputation(WebSecurity.CurrentUserName) < (int)@Constants.Privileges.SetBounty)
            {
                <input type="text" id="bounty" name="bounty" disabled="disabled" value="" />
            }
            else
            {
                <input type="text" id="bounty" name="bounty" class="validate[custom[integer],min[100],max[1000]]" value="" />
            }

            <!--TAGS INPUT-->
            <h2 style="line-height: normal; padding-top: 5px;">@QAS_Nekorama.TAGS</h2>
            @{
                string alltag1 = Model.Question.TagName1;
                alltag1 = Model.Question.TagName2 == null ? alltag1 : alltag1 + " , " + Model.Question.TagName2;
                alltag1 = Model.Question.TagName3 == null ? alltag1 : alltag1 + " , " + Model.Question.TagName3;
                alltag1 = Model.Question.TagName4 == null ? alltag1 : alltag1 + " , " + Model.Question.TagName4;
                alltag1 = Model.Question.TagName5 == null ? alltag1 : alltag1 + " , " + Model.Question.TagName5;
            }
            <div class="ui-widget">
                <input type="text" id="alltag" name="tag" value="@alltag1" class="validate[required]" />
                <input type="text" disabled="disabled" class="message" id="message" name="message" style="width: 0; color: transparent; border: none" />
            </div>
            <!--end ui-widget-->
        </div>
        <!--end primary-2-->

        <div id="primary-3">
            <div class="tools">
                @Html.ActionLink(QAS_Nekorama.SAVE, null, null, new { @class = "save-btn", @onclick = "showMess(event);" })
            </div>
            <!--end tools-->
        </div>
        <!--end primary-3-->
    </form>
</div>
<!--end primary-->